#!/bin/bash
cluster_dir=~/redis-cluster
redis_port=$2
redis_name=$1

mkdir -p $cluster_dir/$redis_name
work_dir=$cluster_dir/$redis_name

########################################################################
echo "daemonize yes" >> $work_dir/redis.conf
echo "pidfile $work_dir/redis.pid" >> $work_dir/redis.conf
echo "port $redis_port" >> $work_dir/redis.conf
echo "bind 0.0.0.0" >> $work_dir/redis.conf
echo "timeout 0" >> $work_dir/redis.conf
echo "cluster-enabled yes" >> $work_dir/redis.conf
echo "cluster-config-file $work_dir/nodes.conf" >> $work_dir/redis.conf
echo "cluster-node-timeout 5000" >> $work_dir/redis.conf
echo "save \"\"" >> $work_dir/redis.conf
echo "appendonly no" >> $work_dir/redis.conf
echo "logfile $work_dir/redis.log" >> $work_dir/redis.conf
echo "maxclients 100000" >> $work_dir/redis.conf
echo "生成配置文件完毕"

###########################################################################
echo "redis-server $work_dir/redis.conf" >> $work_dir/start-redis
chmod 755 $work_dir/start-redis
echo "生成启动文件完毕"

###########################################################################
echo "#!/bin/bash" >> $work_dir/stop-redis
echo "work_dir=$work_dir" >> $work_dir/stop-redis
echo "port=$redis_port" >> $work_dir/stop-redis

echo "if [ -f \$work_dir/redis.pid ]" >> $work_dir/stop-redis
echo "then" >> $work_dir/stop-redis
echo "pid=\`head -1 \$work_dir/redis.pid\`" >> $work_dir/stop-redis
echo "kill -9 \$pid" >> $work_dir/stop-redis
echo "echo \$pid已经停止" >> $work_dir/stop-redis
echo "else" >> $work_dir/stop-redis
echo "pid=\`ps -ef |grep \"redis-server 0.0.0.0:\$port\"|grep -v grep|awk '{print \$2}'\`" >> $work_dir/stop-redis
echo "if [ -z \$pid ]" >> $work_dir/stop-redis
echo "then" >> $work_dir/stop-redis
echo "echo 进程不存在" >> $work_dir/stop-redis
echo "exit" >> $work_dir/stop-redis
echo "else" >> $work_dir/stop-redis
echo "kill -9 \$pid" >> $work_dir/stop-redis
echo "echo \$pid已经停止" >> $work_dir/stop-redis
echo "fi" >> $work_dir/stop-redis
echo "fi" >> $work_dir/stop-redis
chmod 755 $work_dir/stop-redis
echo "生成停止文件完毕"

